package com.example.unisys.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.unisys.dao.*;
import com.example.unisys.entity.StuCourse;
import com.example.unisys.entity.StuExam;
import com.example.unisys.entity.Student;
import com.example.unisys.entity.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class TagService extends ServiceImpl<TagMapper, Tag> {

    @Autowired
    StudentMapper studentMapper;

    @Autowired
    StudentHistoryMapper studentHistoryMapper;

    @Autowired
    StuCourseMapper stuCourseMapper;

    @Autowired
    HistStuCourseMapper histStuCourseMapper;

    @Autowired
    StuExamMapper stuExamMapper;

    @Autowired
    HistStuExamMapper histStuExamMapper;

    public int update(Tag tag) {

        //这sb mybatisplus 压根不能用updateQuery
        //mybatisplus 实际上会先寻找xml中对应的语句
//        return
        return baseMapper.updateById(tag);
//        return baseMapper.update(tag, new QueryWrapper<Tag>().lambda()
//                .eq(Tag::getTag_name, tag.getTag_name())
//                .eq(Tag::getTag_id,tag.getTag_id())
//        );
    }

//    public int insertSelective(Tag tag) {
//        return baseMapper.insertSelective(tag);
//    }

    public List<Tag> getTags(HashMap<String ,Object> hs) {
        return baseMapper.getTags(hs);
    }

    public Tag getActiveTag() {
        return baseMapper.getActiveTag();
    }

    public void setTagActiveById(HashMap<String, Object> hs) {
        baseMapper.setTagActiveById(hs);
    }

//    public int deleteTagById(HashMap<String, Object> hs) {
//        return baseMapper.deleteTagById(hs);
//    }

    public void updateSystemData(HashMap<String, Object> hs) {
        //后续采用容错率更高的java逻辑

        //学生数据迁移 stu_table 数据全部迁移到 stu_history_table,若stu_table不为空
        if (studentMapper.selectCount(new QueryWrapper<>()) > 0) {
            //迁移学生
            studentHistoryMapper.migrateFromStu();
            //删除学生
            studentMapper.delete(
                    new QueryWrapper<Student>().lambda().ne(Student::getTag, hs.get("tag_id")));
        }
        //学生选课数据迁移 stu_course_table 数据迁移到 history_stu_course_table,先判断数据是否为空
        if (stuCourseMapper.selectCount(new QueryWrapper<>()) > 0) {
            histStuCourseMapper.migrateFromStuCourse();
            stuCourseMapper.delete(
                    new QueryWrapper<StuCourse>().lambda().ne(StuCourse::getTag, hs.get("tag_id")));
        }
        //学生考卷迁移，
        if (stuExamMapper.selectCount(new QueryWrapper<>()) > 0) {
            histStuExamMapper.migrateFromStuExam();
            stuExamMapper.delete(
                    new QueryWrapper<StuExam>().lambda().ne(StuExam::getTag, hs.get("tag_id")));
        }
//        baseMapper.updateSystemData(hs);
    }

    public Integer getTagsSum() {
        return baseMapper.selectCount(new QueryWrapper<>());
    }
}
